package com.zb.code.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.zb.code.service.ConCodePrintTemplateService;
import com.zb.entity.*;
import com.zb.result.PageUtil;
import com.zb.secure.BCryptUtil;
import com.zb.service.*;
import com.zb.vo.CodeDataPrintSelectVo;
import com.zb.vo.PrintCodeDataVo;
import com.zb.vo.PrintTemplateDataVo;
import com.zb.yaml.EnDeSecret;
import com.zb.yaml.MobilePath;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author WEN
 * @Date 2023/9/23 0023
 **/
@Service
public class ConCodePrintTemplateServiceImpl implements ConCodePrintTemplateService {
    @Resource
    private ZjCodeBatchGoodsService codeBatchGoodsService;
    @Resource
    private ZjCodeService codeService;
    @Resource
    private ZjCodePrintTemplateComponentService codePrintTemplateComponentService;
    @Resource
    private ZjProductService productService;
    @Resource
    private ZjProductSpecService productSpecService;


    // 获取打印模板数据
    @Override
    public PrintTemplateDataVo getPrintTemplateData(CodeDataPrintSelectVo selectVo) {
        PageUtil.setPageLimit(selectVo);
        List<ZjCode> codeList = codeService.getByBatchGoodsId(selectVo.getBatchGoodsId(), selectVo.getPage(), selectVo.getLimit());
        ZjCodeBatchGoods batchGoods = codeBatchGoodsService.getById(selectVo.getBatchGoodsId());
        Assert.isTrue(null != batchGoods, "批次编码商品ID参数["+ selectVo.getBatchGoodsId() +"]的数据不存在");
        ZjProduct product = productService.getById(batchGoods.getGoodsId());
        ZjProductSpec spec = productSpecService.getById(batchGoods.getGoodsSpecId());
        PrintTemplateDataVo dataPrintVo = new PrintTemplateDataVo();
        dataPrintVo.setGoodsId(product.getId());
        dataPrintVo.setBrandId(product.getBrandId());
        dataPrintVo.setBrand(product.getBrand());
        dataPrintVo.setModel(product.getModel());
        dataPrintVo.setName(product.getName());
        dataPrintVo.setGoodsSpecId(spec.getId());
        dataPrintVo.setGoodsSpecName(spec.getName());
        dataPrintVo.setTypeId(product.getTypeId());
        dataPrintVo.setType(product.getType());
        dataPrintVo.setGoodsCreateTime(product.getGmtCreate());
        dataPrintVo.setCodeDataVos(codeList.stream().map(code -> {
            PrintCodeDataVo vo = new PrintCodeDataVo();
            vo.setCode(code.getSecurityCode());
            vo.setStoreCode(code.getStoreCode());
            vo.setWarrantyDay(code.getWarrantyDay());
            vo.setCodeCreateTime(code.getGmtCreate());
            vo.setPath(MobilePath.getCodeSecurityUrl().replaceAll("CODE_ID", code.getId().toString()).replaceAll("CUST_CODE", BCryptUtil.aesEncry(EnDeSecret.aesKey(), batchGoods.getCustAccId().toString())));
            return vo;
        }).toList());
        dataPrintVo.setTemplateComponentVos(codePrintTemplateComponentService.getByPrintTemplateId(selectVo.getPrintTempLateId()));
        return dataPrintVo;
    }
}
